package com.easylinkin.linkappapi.lobar.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.ClockConfig;
import com.easylinkin.linkappapi.lobar.service.ClockConfigService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogOperateType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 考勤规则管理
 */
@RestController
@RequestMapping("clockConfig")
@Api(tags = "考勤规则管理")
public class ClockConfigController {
  
  @Autowired
  private ClockConfigService clockConfigService;

  /**
   * @Description: 增加考勤规则
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加考勤规则")
  @CommonOperateLogAnnotate(module = LogModule.LABOR_MANAGEMENT, desc = "")
  public RestMessage insert(@RequestBody ClockConfig clockConfig){
    ClockConfig old = clockConfigService.findNew();
    if(null != old){
      LogHelper.setContent(LogOperateType.LABOL_CLOCK_CONFIG_UPDATE,
          old.getCountType() == 1 ? "末减初":"时间段累加",
          old.getClockType() == 1 ? "一天一打卡" : "一天两打卡",
          clockConfig.getCountType() == 1 ? "末减初":"时间段累加",
          clockConfig.getClockType() == 1 ? "一天一打卡" : "一天两打卡");
    }
    clockConfigService.insert(clockConfig);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除考勤规则（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除考勤规则（包含批量删除）")
  public RestMessage delBatch(@RequestBody ClockConfig clockConfig){
    Assert.notEmpty(clockConfig.getIds(),"id不能为空");
    clockConfigService.removeByIds(clockConfig.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑考勤规则
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑考勤规则")
  public RestMessage updateById(@RequestBody ClockConfig clockConfig){
    Assert.notNull(clockConfig.getId(),"id不能为空");
    clockConfigService.updateOne(clockConfig);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询考勤规则详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询考勤规则详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    ClockConfig clockConfig = clockConfigService.getById(id);
    return RestBuilders.successBuilder().data(clockConfig).build();
  }

  /**
   * @Description: 查询当前正在执行的考勤规则
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("index")
  @ApiOperation("根据id查询考勤规则详情")
  public RestMessage findNew() {
    ClockConfig clockConfig = clockConfigService.findNew();
    return RestBuilders.successBuilder().data(clockConfig).build();
  }


  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<ClockConfig> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<ClockConfig> record =  clockConfigService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

